package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入书名: ");
        String name = scanner.nextLine();
        System.out.println("请输入作者名: ");
        String author = scanner.nextLine();
        System.out.println("请输入价格: ");
        double price = scanner.nextDouble();
        scanner.nextLine();//如果不添加,会吸收上一行的回车导致无法读取,方法一
        //方法二 更换顺序
        //方法三 使用next忽略回车
        System.out.println("请输入书类型: ");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();
        for(int i = 0;i < currentSize;i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)) {
                    System.out.println("该书已存在 不能重复添加");
                    return;
            }
        }
        //不存在,进行添加
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize + 1);
    }
}
